package com.atguigu.wechat.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.atguigu.model.wechat.Menu;
import com.atguigu.vo.wechat.MenuVo;
import com.atguigu.wechat.mapper.MenuMapper;
import com.atguigu.wechat.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-04-24
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private WxMpService wxMpService;

    /**
     * 获取全部菜单
     *
     * @return {@link Menu}
     */
    @Override
    public List<MenuVo> findMenuInfo() {
        ArrayList<MenuVo> list = new ArrayList<>();
        // 1.查询除所有菜单 list 集合
        List<Menu> menuList = list();

        // 2.查询所有一级菜单 parent_id = 0 ,返回一级菜单 list 集合
        List<Menu> oneMenuList = menuList.stream().filter(menu -> menu.getParentId() == 0).collect(Collectors.toList());

        // 3.一级菜单 list 集合遍历，得到每个一级菜单
        oneMenuList.forEach(oneMenu -> {
            // 一级菜单 --- MenuVo
            MenuVo oneMenuVo = new MenuVo();
            BeanUtils.copyProperties(oneMenu, oneMenuVo);

            // 4.获取每个一级菜单里面所有二级菜单 id 和 parent_id 比较
            // 一级菜单 id 和 其他菜单 parent_id
            List<Menu> twoMenuList = menuList.stream().filter(menu -> menu.getParentId().longValue() == oneMenu.getId()).collect(Collectors.toList());

            // 5.把一级菜单里面所有二级菜单获取到，封装一级菜单 children 集合里面
            // List<Menu> -- List<MenuVo>
            List<MenuVo> children = new ArrayList<>();
            twoMenuList.forEach(twoMenu -> {
                MenuVo towMenuVo = new MenuVo();
                BeanUtils.copyProperties(twoMenu, towMenuVo);
                children.add(towMenuVo);
            });

            // 涉及到 Menu 和 MenuVo 转换
            oneMenuVo.setChildren(children);
            // 把每一个封装好的一级菜单放到最终 list 集合
            list.add(oneMenuVo);
        });
        return list;
    }

    /**
     * 同步菜单
     */
    @Override
    public void syncMenu() {
        // 1.菜单数据查询，封装微信要求的菜单格式
        List<MenuVo> menuVoList = this.findMenuInfo();
        // 菜单
        JSONArray buttonList = new JSONArray();
        for (MenuVo oneMenuVo : menuVoList) {
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            if (CollectionUtils.isEmpty(oneMenuVo.getChildren())) {
                one.put("type", oneMenuVo.getType());
                one.put("url", "https://17m1b77667.goho.co/#" + oneMenuVo.getUrl());
            } else {
                JSONArray subButton = new JSONArray();
                for (MenuVo twoMenuVo : oneMenuVo.getChildren()) {
                    JSONObject view = new JSONObject();
                    view.put("type", twoMenuVo.getType());
                    if ("view".equals(twoMenuVo.getType())) {
                        view.put("name", twoMenuVo.getName());
                        // H5页面地址
                        view.put("url", "https://17m1b77667.goho.co#" + twoMenuVo.getUrl());
                    } else {
                        view.put("name", twoMenuVo.getName());
                        view.put("key", twoMenuVo.getMenuKey());
                    }
                    subButton.add(view);
                }
                one.put("sub_button", subButton);
            }
            buttonList.add(one);
        }
        // 菜单
        JSONObject button = new JSONObject();
        button.put("button", buttonList);
        try {
            // 2.调用工具里面的方法实现菜单推送
            wxMpService.getMenuService().menuCreate(button.toJSONString());
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除菜单
     */
    @Override
    public void removeMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }
}
